<template>
  <div class="wrapper" v-cloak v-if="questionInfo">
    <el-col :span="4" class="stu-info">
      <div class="stu-info-div">
        <p>姓名:</p>
        <el-input
          v-popover:popover
          ref="nameInput"
          v-model="studentName"
          placeholder="请输入姓名"
        ></el-input>
      </div>
      <div class="stu-info-div">
        <p>毕业年份:</p>
        <el-date-picker
          v-model="finishYears"
          type="year"
          placeholder="清选择毕业年份"
        >
        </el-date-picker>
      </div>
    </el-col>
    <el-col :span="16">
      <div class="questionnaire-item">
        <h1 class="title1">{{ questionInfo.title }}</h1>
        <h3 class="title2">{{ questionInfo.peopleKind }}</h3>
        <h2 class="title3">
          {{ questionInfo.tips }}
        </h2>
        <p class="tip-title">在进行调查之前，需要明确以下几个问题：</p>
        <div style="margin-bottom: 20px">
          <p class="tip-title" style="margin-top: 15px">
            一、什么是复杂工程问题？
          </p>
          <p class="tip-content"></p>
          <p class="tip-content">
            复杂工程问题必须具备下述特征①，同时具备下述特征②-⑦的部分或全部。
          </p>

          <p class="tip-content">
            ①必须运用深入的工程原理，经过分析才可能得到解决;
          </p>

          <p class="tip-content">
            ②涉及多方面的技术、工程和其它因素，并可能相互有一定冲突;
          </p>

          <p class="tip-content">
            ③需要通过建立合适的抽象模型才能解决，在建模过程中需要体现出创造性;
          </p>

          <p class="tip-content">④不是仅靠常用方法就可以完全解决的;</p>
          <p class="tip-content">
            ⑤问题中涉及的因素可能没有完全包含在专业工程实践的标准和规范中;
          </p>
          <p class="tip-content">⑥问题相关各方利益不完全一致;</p>
          <p class="tip-content">
            ⑦具有较高的综合性，包含多个相互关联的子问题。
          </p>
        </div>
        <div style="margin-bottom: 20px">
          <p class="tip-title">二、自我评估</p>
          <p class="tip-content">
            评估自己各项毕业要求指标点的达成情况（A—非常满意，B—满意，C一般，D—不满意）
          </p>
        </div>
        <div style="margin-bottom: 20px">
          <p class="tip-title">三、重要性</p>
          <p class="tip-content">
            评估各项毕业要求指标点对从事<span v-if="id == 1">通信</span
            ><span v-if="id == 2">软件</span
            ><span v-if="id == 3">计算机</span
            >工程工作的重要性程度（A—非常重要，B—重要，C—一般，D—不重要）
          </p>
        </div>
        <div style="margin-bottom: 20px">
          <p class="tip-title">四、学校培养</p>
          <p class="tip-content">
            评估你对学校在该项内容培养的满意程度（A—非常满意，B—满意，C一般，D—不满意）
          </p>
        </div>
        <p
          class="tip-title"
          style="font-size: 20px; color: red; margin-bottom: 10px"
        >
          本部分调查问卷具体内容：
        </p>
        <div
          class="question-item"
          v-for="(questionItem, questionIndex) in questionInfo.question"
          :key="questionIndex"
        >
          <p class="question-title">
            <span style="white-space: nowrap">{{ questionIndex + 1 }}、</span>
            <span> {{ questionItem.title }}</span>
          </p>
          <div
            v-for="(childItem, childIndex) in questionItem.children"
            :key="childIndex"
            class="child-item"
            :class="{
              'error-item': childItem.error,
            }"
          >
            <div class="question-name">
              <span class="q-index"
                >{{ questionIndex + 1 }}.{{ childIndex + 1 }}</span
              >
              <span> {{ childItem.title }} </span>
            </div>

            <div class="radio-table">
              <div class="table-header">
                <p class="empty"></p>
                <p class="header-text">A</p>
                <p class="header-text">B</p>
                <p class="header-text">C</p>
                <p class="header-text">D</p>
              </div>
              <div class="radio-item">
                <p>自我评估</p>
                <el-radio-group
                  v-model="childItem.result1"
                  text-color="'#2a3783'"
                  :disabled="btnText == '已提交'"
                >
                  <el-radio :label="'A'"></el-radio>
                  <el-radio :label="'B'"></el-radio>
                  <el-radio :label="'C'"></el-radio>
                  <el-radio :label="'D'"></el-radio>
                </el-radio-group>
              </div>
              <div class="radio-item">
                <p>重要性</p>

                <el-radio-group
                  v-model="childItem.result2"
                  text-color="'#2a3783'"
                  :disabled="btnText == '已提交'"
                >
                  <el-radio :label="'A'"></el-radio>
                  <el-radio :label="'B'"></el-radio>
                  <el-radio :label="'C'"></el-radio>
                  <el-radio :label="'D'"></el-radio>
                </el-radio-group>
              </div>

              <div class="radio-item">
                <p>学校培养</p>

                <el-radio-group
                  v-model="childItem.result3"
                  text-color="'#2a3783'"
                  :disabled="btnText == '已提交'"
                >
                  <el-radio :label="'A'"></el-radio>
                  <el-radio :label="'B'"></el-radio>
                  <el-radio :label="'C'"></el-radio>
                  <el-radio :label="'D'"></el-radio>
                </el-radio-group>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="footer-btn">
        <button
          @click="handleSubmit"
          v-loading.fullscreen.lock="fullscreenLoading"
        >
          {{ btnText }}
        </button>
      </div>
    </el-col>
  </div>
</template>

<script>
import { getExamDetails, postExamData } from "../../../api/home.js";

export default {
  name: "QuestionnaireItem",
  components: {},
  data() {
    return {
      questionInfo: null,
      studentName: "",
      finishYears: "",
      btnText: "提交问卷",
      fullscreenLoading: false,
    };
  },
  computed: {},
  props: {
    id: [Number, String],
  },
  created() {
    this.getQuestionDetails();
  },
  methods: {
    async getQuestionDetails() {
      console.log(this.id);
      const res = await getExamDetails({ examId: this.id });
      if (res.status == "200") {
        this.initActiveQuestionInfo(res.data);
      }
    },
    initActiveQuestionInfo(questionInfo) {
      if (questionInfo) {
        let questionInfoTmp = JSON.parse(JSON.stringify(questionInfo));
        let infoArr = questionInfoTmp.question;
        for (let i = 0, len = infoArr.length; i < len; i++) {
          for (let j = 0, len1 = infoArr[i].children.length; j < len1; j++) {
            infoArr[i].children[j]["result1"] = "";
            infoArr[i].children[j]["result2"] = "";
            infoArr[i].children[j]["result3"] = "";
            infoArr[i].children[j]["error"] = false;
          }
        }
        this.questionInfo = questionInfoTmp;
      }
    },
    handleSubmit() {
      if (this.btnText == "已提交") {
        return;
      }
      let flag = true;
      let resArr = [];
      if (!this.studentName) {
        flag = false;
        this.$refs.nameInput.focus();
        this.$message.error("请在左侧姓名之后再进行提交");
      }else if(!this.finishYears){
        flag = false;
        this.$message.error("请在左侧选择毕业年份之后再进行提交");
      } else {
        if (this.questionInfo) {
          let questionInfoTmp = this.questionInfo;
          let infoArr = questionInfoTmp.question;
          for (let i = 0, len = infoArr.length; i < len; i++) {
            for (let j = 0, len1 = infoArr[i].children.length; j < len1; j++) {
              if (
                !infoArr[i].children[j]["result1"] ||
                !infoArr[i].children[j]["result2"] ||
                !infoArr[i].children[j]["result3"]
              ) {
                flag = false;
                infoArr[i].children[j]["error"] = true;
              } else {
                resArr.push({
                  id: infoArr[i].children[j].id,
                  result1: infoArr[i].children[j]["result1"],
                  result2: infoArr[i].children[j]["result2"],
                  result3: infoArr[i].children[j]["result3"],
                });
                infoArr[i].children[j]["error"] = false;
              }
            }
          }
        }
        if (!flag) {
          this.$notify.error({
            title: "提示",
            message: "请完整填写调查问卷",
          });
        }
      }
      if (flag) {
        this.postResult(resArr);
      }
    },
    async postResult(resList) {
      this.fullscreenLoading = true;
      const res = await postExamData({
        examId: this.id,
        studentName: this.studentName,
        resList: JSON.stringify({ data: resList }),
        finishYears:this.finishYears
      });
      if (res.status == "200") {
        this.fullscreenLoading = false;
        this.btnText = "已提交";
        this.$alert("提交成功", "提示", {
          confirmButtonText: "确定",
          callback: (action) => {},
        });
      } else {
        this.fullscreenLoading = false;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  // align-items: center;
  // justify-content: center;
  background: #f3f3f3;
}
.questionnaire-item {
  color: #000;
  height: 850px;
  overflow: auto;
  padding: 30px;
  background: #fff;
  box-sizing: border-box;
  h1,
  h2,
  h3 {
    font-weight: 500;
  }
  .title1 {
    text-align: center;
    font-size: 26px;
    line-height: 35px;
    color: #2a3783;
  }
  .title2 {
    text-align: center;
    font-size: 18px;
    line-height: 35px;
    padding-bottom: 10px;
  }
  .title3 {
    font-size: 18px;
    line-height: 28px;
    text-indent: 2rem;
    padding-bottom: 10px;
  }
  .tip-title {
    font-size: 18px;
    line-height: 30px;
    text-indent: 2rem;
  }
  .tip-content {
    font-size: 16px;
    line-height: 32px;
    text-indent: 4rem;
  }
  .question-item {
    .question-title {
      margin-left: 2rem;
      display: flex;
      margin-bottom: 5px;
      span {
        font-size: 18px;
        font-weight: 500;
        line-height: 30px;
      }
    }
    .child-item {
      padding-bottom: 20px;
      .question-name {
        display: flex;
        margin-left: 2.5rem;

        span {
          display: inline-block;
          margin-right: 5px;
          font-size: 16px;
          line-height: 24px;
        }
        .q-index {
          white-space: nowrap;
        }
      }
      .radio-table {
        margin-top: 20px;

        border: 1px solid #aaaaaa;

        width: 300px;
        margin-left: 4rem;
        .table-header {
          display: flex;
          border-bottom: 1px solid #dddddd;

          .empty {
            width: 100px;
          }
          .header-text {
            padding: 15px 0;
            width: 50px;
            text-align: center;
          }
        }
        .radio-item {
          display: flex;
          align-items: center;
          border-bottom: 1px solid #dddddd;

          p {
            width: 100px;
            padding: 10px;
            box-sizing: border-box;
            text-align: justify;
          }
          .el-radio-group {
            flex: 1;
            display: flex;
            .el-radio {
              flex: 1;
              margin-right: 0;
              text-align: center;
            }
          }
        }
      }
    }
    .error-item {
      .radio-table {
        border: 1px solid red;
      }
    }
  }
}
.footer-btn {
  width: 1200px;
  display: flex;
  justify-content: center;
  margin-top: 20px;
  button {
    width: 300px;
    height: 50px;
    font-size: 20px;
    background: #0c7ed3;
    color: #fff;
    border-radius: 10px;
    cursor: pointer;
  }
}
.stu-info {
  .stu-info-div {
    display: flex;
    align-items: center;
    height: fit-content;
    margin-top: 50px;
    padding: 0 40px;
    box-sizing: border-box;
    p {
      width: 150px;
      white-space: nowrap;
      margin-right: 20px;
      font-size: 20px;
    }
  }
}
</style>
